package com.github.freewu.mvp.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.freewu.mvp.bean.Mvp;
import com.github.freewu.mvp.mvpInterface.MvpInterface;
import com.github.freewu.mvp.mvpInterface.ViewInit;
import com.github.freewu.mvp.presenter.Presenter;
import com.github.freewu.mvp.view.status.SimpleStatusView;
import com.github.freewu.mvp.view.status.StatusController;
import com.lqkj.commons.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 * Created by 杨哲 on 2016/4/21 0021.
 */
public abstract class BaseActivity extends AppCompatActivity implements ViewInit,MvpInterface.ViewInterface {

    protected Context mContext;

    private LinearLayout mLinearLayout;

    private StatusView statusView;

    private View mView;

    private Toolbar toolbar;

    private TextView toolbarTitle;

    protected List<Presenter> presenters=new LinkedList<>();

    protected final String LEFT="left";
    protected final String CENTER="center";
    protected final String RIGHT="right";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        mContext=new ContextThemeWrapper(getApplicationContext(), Mvp.config.getmStyle());

        setContentView(R.layout.base_activity_layout);

        StatusBarCompat.compat(this,getResources().getColor(Mvp.config.getBaseColor()));

        initToolBar();

        initMView();

        initView(mView);

        setStatusViewListener();

        initData();

        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onCreate();
            }
        }
        if (statusView!=null){
            statusView.onCreate();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onDestroy();
            }
        }
        if (statusView!=null){
            statusView.onDestroy();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onPause();
            }
        }
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onCreateView();
            }
        }
        return super.onCreateView(parent, name, context, attrs);
    }

    /**
     * 初始化toolbar
     */
    private void initToolBar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.keyboard_arrow_left);
        toolbar.setBackgroundColor(getResources().getColor(Mvp.config.getBaseColor()));
        toolbar.setTitle("");

        setSupportActionBar(toolbar);
        //监听返回键
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivity.this.finish();
            }
        });
    }

    /**
     * 隐藏toolbar
     */
    public void hideToolBar(){
        toolbar.setVisibility(View.GONE);
    }

    /**
     * 显示toolbar
     */
    public void showToolBar(){
        toolbar.setVisibility(View.VISIBLE);
    }

    private void initMView(){
        mLinearLayout=(LinearLayout)findViewById(R.id.base_linear_layout);

        //如果设置了加载的视图就不用默认的
        StatusView statusView =Mvp.config.getStatusView();

        if (statusView!=null){
            statusView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mLinearLayout.addView(statusView);
        }else if (this.statusView ==null) {
            this.statusView = new SimpleStatusView(getContext());
            this.statusView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            mLinearLayout.addView(this.statusView);
        }
        //初始化加载的视图
        int layoutId=getLayout();
        if (layoutId>0) {
            mView = View.inflate(mContext, layoutId, null);
            mView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            this.statusView.setNormalView(mView);
        }
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    /**
     * 设置标题
     * @param title 标题内容
     */
    @Override
    public void setTitle(CharSequence title) {
        if (toolbarTitle==null) {
            TextView textView = new TextView(getContext());

            textView.setTag(CENTER);

            Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            layoutParams.gravity = Gravity.CENTER;

            textView.setLayoutParams(layoutParams);
            textView.setTextColor(Color.WHITE);
            textView.setTextSize(20);

            toolbarTitle = textView;

            toolbar.addView(toolbarTitle);
        }
        toolbarTitle.setText(title);
    }

    /**
     * 设置标题颜色
     * @param color
     */
    public void setToolBarTitleColor(@ColorRes int color){
        if (toolbarTitle!=null) {
            toolbarTitle.setTextColor(getResources().getColor(color));
        }
    }

    /**
     * 设置toolbar左侧视图
     * @param view
     */
    public void setToolbarLeftView(View view){
        View leftView=toolbar.findViewWithTag(LEFT);

        if (leftView!=null){
            toolbar.removeView(leftView);
        }

        view.setTag(LEFT);

        Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) view.getLayoutParams();
        layoutParams.gravity=Gravity.LEFT;
        toolbar.addView(view);
    }

    /**
     * 设置toolbar中心视图，会删除标题后添加
     * @param view
     */
    public void setToolbarCenterView(View view){
        View centerView=toolbar.findViewWithTag(CENTER);

        if (centerView!=null){
            toolbar.removeView(centerView);
        }

        view.setTag(CENTER);

        Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) view.getLayoutParams();
        layoutParams.gravity=Gravity.CENTER;
        toolbar.addView(view);
    }

    /**
     * 设置toolbar右侧视图
     * @param view
     */
    public void setToolbarRightView(View view){
        View rightView=toolbar.findViewWithTag(RIGHT);

        if (rightView!=null){
            toolbar.removeView(rightView);
        }

        view.setTag(RIGHT);

        Toolbar.LayoutParams layoutParams = (Toolbar.LayoutParams) view.getLayoutParams();
        layoutParams.gravity=Gravity.RIGHT;
        toolbar.addView(view);
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public StatusView getStatusView() {
        return statusView;
    }

    @Nullable
    @Override
    public View findViewById(@IdRes int id) {
        if (mView!=null){
            return mView.findViewById(id);
        }else {
            return super.findViewById(id);
        }
    }
    @Override
    public StatusController getStatusController(){
        return getStatusView();
    }

    @Override
    public void addPresenter(Presenter presenter) {
        this.presenters.add(presenter);
    }

    @Override
    public void OnReloadViewClick(View v) {

    }

    @Override
    public void OnErrorViewClick(View v) {

    }

    protected void setStatusViewListener(){
        getStatusView().getReloadView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnReloadViewClick(v);
            }
        });
        getStatusView().getErrorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnErrorViewClick(v);
            }
        });
    }
}
